package com.atguigu.smsservice.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.atguigu.commonutils.utils.RandomUtils;
import com.atguigu.smsservice.service.SmsService;
import com.google.common.collect.Maps;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @author alan
 */
@Service
public class SmsServiceImpl implements SmsService {

    @Value("${aliyun.oss.access-key-id}")
    private String accessKeyId;
    @Value("${aliyun.oss.access-key-secret}")
    private String accessSecret;
    @Value("${aliyun.sms.sign-name}")
    private String signName;
    @Value("${aliyun.sms.template-code}")
    private String registerTemplateParam;
    @Value("${aliyun.sms.template-code-login}")
    private String loginTemplateParam;
    @Resource
    private RedisTemplate<String, String> jsonRedisTemplate;

    @Override
    public boolean sendRegisterCode(String phone) {
        String code = RandomUtils.numCode(4);
        if (sendCode(phone, registerTemplateParam, code)) {
            jsonRedisTemplate.opsForValue().set(String.format("sms:phone:register:%s", phone), code, 5, TimeUnit.MINUTES);
            return true;
        }
        return false;
    }

    @Override
    public boolean sendLoginCode(String phone) {
        String code = RandomUtils.numCode(4);
        if (sendCode(phone, loginTemplateParam, code)) {
            jsonRedisTemplate.opsForValue().set(String.format("sms:phone:login:%s", phone), code, 5, TimeUnit.MINUTES);
            return true;
        }
        return false;
    }

    private boolean sendCode(String phone, String templateCode, String code) {
        Map<String, String> param = Maps.newHashMap();
        param.put("code", code);

        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, accessSecret);
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        request.setSysMethod(MethodType.POST);
        request.setSysDomain("dysmsapi.aliyuncs.com");
        request.setSysVersion("2017-05-25");
        request.setSysAction("SendSms");
        request.putQueryParameter("RegionId", "cn-hangzhou");
        request.putQueryParameter("PhoneNumbers", phone);
        request.putQueryParameter("SignName", signName);
        request.putQueryParameter("TemplateCode", templateCode);
        request.putQueryParameter("TemplateParam", JSONObject.toJSONString(param));
        try {
            client.getCommonResponse(request);
            System.out.println("短信基本信息 = " + request.getSysQueryParameters());
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

}
